﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Entity.CommonEnums;
using Entity;
using System.Text.RegularExpressions;

namespace Web.Commons.Helpers
{
    /// <summary>
    /// 前台HtmlHelper扩展方法
    /// </summary>
    public static class ViewHelper
    {

        #region 是否 单选按钮

        /// <summary>
        /// 是否单选按钮
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue">选中的项</param>
        /// <returns></returns>
        public static MvcHtmlString YesOrNoForRadioButton(this HtmlHelper html, int selectedValue)
        {
            StringBuilder sb = new StringBuilder();
            Dictionary<string, int> YesOrNoDic = new Dictionary<string, int>() {
             {"是",1},
             {"否",0},
            };
            sb.Append("<select name='YesOrNo' id='YesOrNo'>");
            foreach (KeyValuePair<string, int> i in YesOrNoDic)
                if (i.Value == selectedValue)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", i.Value, i.Key);
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", i.Value, i.Key);
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 是否单选按钮
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString YesOrNoForRadioButton(this HtmlHelper html)
        {
            return YesOrNoForRadioButton(html, 0);
        }

        #endregion 是否 单选按钮

        #region 字符截取
        /// <summary>
        /// 截取表述的长度
        /// string类型的扩展方法
        /// </summary>
        /// <param name="str">描述</param>
        /// <param name="length">最大长度</param>
        /// <returns></returns>
        public static string GetSummaryString(this string sInString, int iCutLength)
        {
            if (string.IsNullOrEmpty(sInString)) return "";
            sInString = sInString.Replace(" ", "");
            sInString = System.Text.RegularExpressions.Regex.Replace(sInString, @"(<[a-zA-Z]+[^>]*>)|(</[a-zA-Z\d]+>)|(<!--[^~]*-->)|(&nbsp;)", "");
            if (sInString == null || sInString.Length == 0 || iCutLength <= 0) return "";
            int iCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
            if (iCount > iCutLength)
            {
                int iLength = 0;
                for (int i = 0; i < sInString.Length; i++)
                {
                    int iCharLength = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[] { sInString[i] });
                    iLength += iCharLength;
                    if (iLength == iCutLength)
                    {
                        sInString = sInString.Substring(0, i + 1) + "...";
                        break;
                    }
                    else if (iLength > iCutLength)
                    {
                        sInString = sInString.Substring(0, i);
                        break;
                    }
                }
            }
            return sInString;
        }

        /// <summary>
        /// 从List集合转成字符串
        /// </summary>
        /// <param name="list">集合</param>
        /// <param name="split">分隔符号</param>
        /// <returns></returns>
        public static string FromListToString(this List<string> list, char split)
        {
            string res = null;
            list.ForEach(i => res += i + ",");
            if (list.Count > 0) res = res.Remove(res.Length);
            return res;
        }
        #endregion

        #region 证件类型
        /// <summary>
        /// 证件类型下拉列表
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString IDTypeForSelectList(this HtmlHelper html, int selectedValue)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='IDType' id='IDType' class='input' style='height:20px;'>");
            foreach (IDType i in Enum.GetValues(typeof(IDType)))
                if ((int)i == selectedValue)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((IDType)i).GetDescription());
                else
                {
                    sb.AppendFormat("<option value='{0}'>{1}</option>", (int)i, ((IDType)i).GetDescription());
                }
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 状态字段下列列表框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString IDTypeForSelectList(this HtmlHelper html)
        {
            return IDTypeForSelectList(html, 1);
        }
        #endregion

        #region 状态字段下列列表框
        /// <summary>
        /// 状态字段下列列表框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue">选中的项</param>
        /// <returns></returns>
        public static MvcHtmlString StatusForSelectList(this HtmlHelper html, int selectedValue)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='Status' id='Status'>");
            foreach (Status i in Enum.GetValues(typeof(Status)))
                if ((int)i == selectedValue)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription());
                else
                {
                    if (i == Status.Normal) sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((Status)i).GetDescription());
                    else
                        sb.AppendFormat("<option value='{0}'>{1}</option>", (int)i, ((Status)i).GetDescription());
                }
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        /// <summary>
        /// 状态字段下列列表框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString StatusForSelectList(this HtmlHelper html)
        {
            return StatusForSelectList(html, 100);
        }

        #endregion 状态字段下列列表框

        #region 支付方式下拉框

        /// <summary>
        /// 支付方式下列框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString PayTypeSelectList(this HtmlHelper html)
        {
            return PayTypeSelectList(html, 0);
        }

        /// <summary>
        /// 支付方式下列框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue">要选中的项</param>
        /// <returns></returns>
        public static MvcHtmlString PayTypeSelectList(this HtmlHelper html, byte selectedValue)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select name='PayType' id='PayType'>");
            foreach (PayType i in Enum.GetValues(typeof(PayType)))
                if ((int)i == selectedValue && selectedValue != 0)
                    sb.AppendFormat("<option value='{0}' selected='selected'>{1}</option>", (int)i, ((PayType)i).GetDescription());
                else
                    sb.AppendFormat("<option value='{0}'>{1}</option>", (int)i, ((PayType)i).GetDescription());
            sb.Append("</select>");
            return MvcHtmlString.Create(sb.ToString());
        }

        #endregion 支付方式下拉框

        #region 支付方式复选框
        /// <summary>
        /// 支付方式复选框
        /// </summary>
        /// <param name="html"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static MvcHtmlString PayTypeForCheckBox(this HtmlHelper html, byte selectedValue)
        {
            StringBuilder sb = new StringBuilder();
            foreach (PayType item in Enum.GetValues(typeof(PayType)))
                if (((PayType)selectedValue & item) != 0)
                    sb.Append("<input type=checkbox name='PayType' id='PayType" + item + "' checked='checked' value='" + (int)item + "' ><label for='PayType" + item + "'><font color='red'>" + item.GetDescription() + "</font></label>");
                else
                    sb.Append("<input type=checkbox name='PayType' id='PayType" + item + "' value='" + (int)item + "'><label for='PayType" + item + "'>" + item.GetDescription() + "</label>");
            return MvcHtmlString.Create(sb.ToString());
        }
        /// <summary>
        /// 支付方式复选框
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString PayTypeForCheckBox(this HtmlHelper html)
        {
            return PayTypeForCheckBox(html, 0);
        }
        #endregion


        /// <summary>
        /// 转换为HTML
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString Raw2(this HtmlHelper html, string str)
        {
            str = Regex.Replace(str, @"(\<|\s+)o([a-z]+\s?=)", "$1$2", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, @"(script|frame|form|meta|behavior|style)([\s|:|>])+", "$1.$2", RegexOptions.IgnoreCase);
            return MvcHtmlString.Create(str);
        }
    }
}